package org.smallreunion.controller.bbs;

import org.slim3.controller.Navigation;
import org.slim3.controller.validator.Validators;
import org.slim3.util.RequestMap;
import org.smallreunion.controller.AbstractController;
import org.smallreunion.form.BBSPostForm;
import org.smallreunion.service.BBSService;

/**
 * BBSEntryController
 *
 * @author Sinmetal
 *
 */
public class EntryController extends AbstractController {

    /** BBSService */
    private BBSService service = new BBSService();

    /**
     * BBSPost登録
     */
    @Override
    public Navigation run() throws Exception {
        RequestMap map = new RequestMap(request);
        Validators v = createValidators(map);
        if (!v.validate()) {
            return redirect(basePath);
        }

        BBSPostForm form = BBSPostForm.getInstance(map);
        long twitterId = getTwitterId();
        service.entry(form, twitterId);
        return redirect(basePath);
    }

    /**
     * Validators作成
     *
     * @param map
     * @return
     */
    private Validators createValidators(RequestMap map) {
        Validators v = new Validators(map);
        v.add("content", v.required());
        return v;
    }
}
